package com.nefu.huiyi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.nefu.huiyi.entity.Doctor;
import com.nefu.huiyi.result.HttpResult;
import com.nefu.huiyi.service.DoctorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/doctors")
@Tag(name = "医师管理接口")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @PostMapping("/new")
    @Operation(summary = "新增医师")
    public HttpResult<Doctor> saveDoctor(@RequestBody Doctor doctor) {
        Doctor savedDoctor = doctorService.saveDoctor(doctor);
        return HttpResult.success(savedDoctor);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除医师")
    public HttpResult<Boolean> deleteDoctor(@PathVariable Integer id) {
        boolean result = doctorService.deleteDoctor(id);
        return result ? HttpResult.success(true) : HttpResult.failed("删除失败");
    }

    @PutMapping
    @Operation(summary = "更新医师信息")
    public HttpResult<Doctor> updateDoctor(@RequestBody Doctor doctor) {
        Doctor updatedDoctor = doctorService.updateDoctor(doctor);
        return HttpResult.success(updatedDoctor);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询医师")
    public HttpResult<Doctor> getDoctorById(@PathVariable Integer id) {
        Doctor doctor = doctorService.getDoctorById(id);
        return HttpResult.success(doctor);
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有医师")
    public HttpResult<List<Doctor>> findAll() {
        List<Doctor> doctors = doctorService.findAll();
        return HttpResult.success(doctors);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询医师列表")
    public HttpResult<IPage<Doctor>> getDoctorPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        Page<Doctor> page = new Page<>(pageNum, pageSize);
        IPage<Doctor> doctorPage = doctorService.getDoctorPage(page, keyword);
        return HttpResult.success(doctorPage);
    }


}